import express from "express";
import db from "../db/db.js";
import allDataSearch from "../common/allDataSearch.js";

const inquireAssetRecord = express.Router();

const detailData = async (params: any) => {
    return new Promise((resolve, reject) => {
        // Step 1: 查询是否存在指定的 fundName
        let selectSql = `SELECT curAsset, date, principal, firstHoldTime, code FROM asset WHERE fundName = ? ORDER BY date DESC LIMIT 1`;

        db.query(selectSql, [params.name], (err, result) => {
            if (err) {
                reject(err);
            }
            resolve(result);
        });
    });
};

inquireAssetRecord.post("/assetListData", async (req, res) => {
    try {
        const params = req.body;
        const dataRes: any = Object.keys(params).length === 0 ? await allDataSearch('asset') : await detailData(params);
        res.send({
            code: 200,
            message: "修改忽略状态完成 请求成功！",
            result: dataRes,
            // total: dataRes.length,
        });
    } catch (error) {
        console.error("Error fetching file records:", error);
        res.status(500).send({
            code: 1,
            message: "修改忽略状态完成 请求失败！",
            result: null,
        });
    }
});

export default inquireAssetRecord;
